package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.Priority;

/**
 * 通用异常处理，springmvc
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(LyException.class)
    public ResponseEntity<ExceptionResult> handleException(LyException e){
        //直接写死不好
        //return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        ExceptionEnums exceptionEnums = e.getExceptionEnums();
        ExceptionResult exceptionResult = new ExceptionResult(exceptionEnums);
        return ResponseEntity.status(exceptionResult.getStatus()).body(exceptionResult);

    }

}
